/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveCommands
{
	class GrooveAddTool : CommandUtils.GrooveUtilBase
	{
		private string m_SpaceURI;
		private string m_Name;
		private string m_ComponentResourceURL;

		[STAThread]
		static void Main(string[] i_Arguments)
		{
			CommandUtils.UtilAppMain.RunApplication(new GrooveAddTool(i_Arguments));
		}

		public GrooveAddTool(string[] i_Arguments)
			: base (i_Arguments)
		{
			base.UsageMessage = "GrooveAddTool [PARAMS...]";
			base.HelpMessage = 
				"GrooveAddTool\r\n\t" + 
				"Adds a tool to specified Groove workspace\r\n" +
				"Parameters:\r\n\t" +
				"/Space=                The Web Services URI for the Groove space\r\n\t" +
				"/Name=                 Name for the new Groove tool\r\n\t" +
				"/ComponentResourceURL  The url for the tool template\r\n\t" +
				"[/Account=             The Web Services URI for the Groove account]\r\n\t" +
				"[/Identity=            The Web Services URI for the Groove identity]\r\n\t" +
				"[/RequestKey=          Token used to communicate Groove host]\r\n\t" +
				"[/Host=                The remote Groove host name to issue commands to]\r\n\t" +
				"[/Port=                The remote Groove host port to issue commands to]\r\n\t" +
				"[/Timeout=             Timeout (in ms) for Web Services method execution]\r\n\t" +
				"[/X=                   Configuration file to use for parameter input]\r\n\t" +
				"\r\n" +
				"NOTE:  To get list of all available tool templates, do not specify ComponentResourceURL\r\n" +
				"\r\n";

		}

		public override CheckResult CheckParameters()
		{
			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded )
				return CheckResult;

			m_SpaceURI = base.GetParameterValue("Space");
			if (System.String.IsNullOrEmpty(m_SpaceURI))
			{
				base.DisplayUsage("You must specify a space.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_ComponentResourceURL = base.GetParameterValue("ComponentResourceURL");
			if (!System.String.IsNullOrEmpty(m_ComponentResourceURL))
			{
				m_Name = base.GetParameterValue("Name");
				if (System.String.IsNullOrEmpty(m_Name))
				{
					base.DisplayUsage("You must specify a name for the tool.");
					return CommandUtils.UtilBase.CheckResult.Failed;
				}
			}

			return base.CheckUnusedParameters();
		}

		public override CommandUtils.UtilBase.CheckResult Run()
		{
			GrooveWebServicesV12Helpers.Space Space = null;

			try
			{
				Space = base.Identity.OpenSpace(m_SpaceURI);
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Unable to open space: " + Exception.Message);
				throw Exception;
			}

			if (System.String.IsNullOrEmpty(m_ComponentResourceURL))
			{
				base.StandardOutput.WriteLine("<ToolTemplates>");

				GrooveWebServicesV12Helpers.ToolTemplate[] ToolTemplates = Space.ToolTemplates;
				foreach (GrooveWebServicesV12Helpers.ToolTemplate ToolTemplate in ToolTemplates)
				{
					string ToolTemplateOutput = 
						"<ToolTemplate " +
						"Name='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.Name) + "' " +
						"Author='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.Author) + "' " +
						"Category='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.Category) + "' " +
						"ComponentResourceURL='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.ComponentResourceURL) + "' " +
						"Description='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.Description) + "' " +
						"ID='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.ID) + "' " +
						"LongName='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.LongName) + "' " +
						"Type='" + CommandUtils.UtilStatic.XMLEncode(ToolTemplate.Type) + "' " +
						"/>";

					base.StandardOutput.WriteLine(ToolTemplateOutput);
				}

				base.StandardOutput.WriteLine("</ToolTemplates>");
			}
			else
			{
				string ToolURI = null;

				try
				{
					ToolURI = Space.AddTool
						(
						m_Name,
						m_ComponentResourceURL
						);
				}
				catch (System.Exception Exception)
				{
					base.StandardError.WriteLine("Unable to add tool: " + Exception.Message);
					throw Exception;
				}

				base.StandardOutput.WriteLine("<Tool URI='" + ToolURI + "'/>");
			}

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}
	}
}
